package com.aiatomic.couting.account;

import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

import org.apache.http.util.EncodingUtils;

import com.aiatomic.couting.AccountInfoActivity;
import com.aiatomic.couting.IConstant;
import com.aiatomic.couting.R;
import com.aiatomic.couting.util.AppUtils;
import com.aiatomic.couting.util.HttpUtils;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.JavascriptInterface;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

public class FragmentAccountInfoFeedback extends Fragment {
	private View view;
	private AccountInfoActivity activity;
	private WebView webView;
	private  JSInterface js;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.fragment_account_info_feedback, container, false);
		activity = (AccountInfoActivity) getActivity();
		webView = (WebView) view.findViewById(R.id.webView);
		WebSettings settings = webView.getSettings();
		settings.setJavaScriptEnabled(true);
		Map<String, String> params = new HashMap<String, String>();
		params.put("aid", AppUtils.aid);
		params.put("platform", String.valueOf(IConstant.PLATFORM));
		params.put("time", String.valueOf(Calendar.getInstance().getTimeInMillis()));
		params.put("channel", String.valueOf(AppUtils.channel));
		params.put("sign", HttpUtils.md5( params.get("aid") + params.get("time")+params.get("platform")+ params.get("channel") + IConstant.NORMAL_MD5_KEY));
		params.put("ssign", HttpUtils.md5( params.get("aid") + params.get("time")+params.get("platform")+ params.get("channel") + AppUtils.sKey));
		String postData = "";
		for(String key:params.keySet()){
			postData+=(key+"="+params.get(key)+"&");
		}
		webView.postUrl(AppUtils.url + "/v/cAbout/report", EncodingUtils.getBytes(postData, "BASE64"));
		webView.setWebViewClient(new WebViewClient() {
			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				view.loadUrl(url);
				return true;
			}
		});
		bindEvent();
		return view;
	}

	private void bindEvent() {
		js=new JSInterface(activity);
		webView.addJavascriptInterface(js, "report");
	}
}
class JSInterface{
	private AccountInfoActivity ctx;
	
	public JSInterface(AccountInfoActivity ctx) {
		super();
		this.ctx = ctx;
	}

	@JavascriptInterface
	public void showMsg(String msg){
		Toast.makeText(ctx,msg,Toast.LENGTH_LONG).show();
		ctx.onBackPressed();	
	}
}